<?php
namespace app\admin\controller;
use app\common\model\Admin;

class Index extends Common
{
	// 欢迎界面
	public function welcome()
	{
		return $this->fetch();
	}



	// 列表展示
	public function index()
	{
		$field=db('wechat')->select();
      	$this->assign("data",$field);
      	return $this->fetch();
	}
	// 添加界面
	public function add() {
		return view();
	}
	public function add_action() {
		$file = request()->file('img');
		$info = $file->move(ROOT_PATH . 'public' . DS . 'static'.DS.'uploads');
		if($info){

			$data['wechat_name'] = input('post.name');
			$data['wechat_img'] =  $info->getSaveName();
			$data['wechat_author'] = input('post.author');
			$data['wechat_in_date'] = date('Y-m-d H:i:s');

			if(db('wechat')  -> insert($data)) {
				return $this->success('添加成功','admin/index/index');
			} else {
				return $this->error('添加失败','admin/index/index');
			}
		}else{
			// 上传失败获取错误信息
			echo $file->getError();
			return $this->error('添加失败','admin/index/index');
		}
	}
		// 删除界面
	public function del() {

		$where['wechat_id'] = input('id');
		if(db("wechat")->where($where)->delete()){
			return $this->success('删除成功','admin/index/index');
		}else{
			return $this->error('删除失败','admin/index/index');
		}

	}
	/*
	// 修改界面
	 */
	public function edit() {
		$where['wechat_id'] = input('id');
		$item = db('wechat')  -> where($where)->find();
		$this->assign('item',$item);
		return $this->fetch();
	}
	public function edit_action(){
		$where['wechat_id'] = input('id');
		$file = request()->file('img');
		if($file){
			// 移动到框架应用根目录/public/uploads/ 目录下
			$info = $file->move(ROOT_PATH . 'public' . DS . 'static'.DS.'uploads');

			if($where['wechat_id'] && $info){

				$data['wechat_id'] = $where['wechat_id'];
				$data['wechat_name'] = input('post.name');
				$data['wechat_img'] =   $info->getSaveName();
				$data['wechat_author'] = input('post.author');
				$data['wechat_in_date'] = date('Y-m-d H:i:s');

				if(db('wechat')  -> update($data)) {
					return $this->success('修改成功','admin/index/index');
				} else {
					return $this->error('修改失败','admin/index/index');
				}
			}
			else{
				return $this->error('修改失败','admin/index/index');
			}
		}
	}

	// 修改密码
	public function pass()
	{
		if(request()->isPost())
		{
			$res=(new Admin())->pass(input('post.'));
			if($res['id']){
				$this->success($res['msg'],'admin/login/login');exit;
			}else{
				$this->error($res['msg']);exit;
			}
		}

		return $this->fetch();
	}

}
